package com.test.ad.demo;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.test.ad.demo.model.AdModel;
import com.test.ad.demo.util.AdCacheUtil;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

/**
 * 收益提示框Dialog
 */
public class RevenueDialog extends Dialog {

    private Context context;
    private double currentRevenue;
    private AdCacheUtil adCacheUtil;
    private DecimalFormat decimalFormat;

    public RevenueDialog(Context context, double currentRevenue) {
        super(context, android.R.style.Theme_Translucent_NoTitleBar);
        this.context = context;
        this.currentRevenue = currentRevenue;
        this.adCacheUtil = new AdCacheUtil(context);
        this.decimalFormat = new DecimalFormat("#.##");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 设置布局
        View view = LayoutInflater.from(context).inflate(R.layout.revenue_dialog, null);
        setContentView(view);
        
        // 完全去除对话框的默认样式和阴影
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
        
        // 设置窗口属性，去除所有默认效果
        getWindow().setDimAmount(0.5f); // 设置背景遮罩透明度
        
        // 初始化视图
        initViews(view);
    }

    private void initViews(View view) {
        TextView tvRevenueAmount = view.findViewById(R.id.tv_revenue_amount);
        TextView tvTodayAmount = view.findViewById(R.id.tv_today_amount);
        TextView tvMonthAmount = view.findViewById(R.id.tv_month_amount);
        Button btnAccept = view.findViewById(R.id.btn_accept);

        // 设置本次收益金额
        tvRevenueAmount.setText("+" + decimalFormat.format(currentRevenue));

        // 计算并设置今日和本月收益
        calculateAndSetEarnings(tvTodayAmount, tvMonthAmount);

        // 设置按钮点击事件
        btnAccept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void calculateAndSetEarnings(TextView tvTodayAmount, TextView tvMonthAmount) {
        // 获取所有广告数据
        List<AdModel> allAds = adCacheUtil.getAllAds();
        
        // 获取今天的日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String today = simpleDateFormat.format(new Date());

        // 获取本月的第一天
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String monthStart = simpleDateFormat.format(calendar.getTime());

        double todayCoins = 0.0;
        double monthCoins = 0.0;

        for (AdModel ad : allAds) {
            String adDate = ad.getDate();
            
            // 计算今日收益
            if (today.indexOf(adDate.split(" ")[0]) > -1) {
                todayCoins += ad.getCoins();
            }
            
            // 计算本月收益
            if (adDate.compareTo(monthStart) >= 0 && adDate.compareTo(today) <= 0) {
                monthCoins += ad.getCoins();
            }
        }

        // 转换为元并显示
        double todayYuan = todayCoins;
        double monthYuan = monthCoins;

        tvTodayAmount.setText(decimalFormat.format(todayYuan));
        tvMonthAmount.setText(decimalFormat.format(monthYuan));
    }
}
